﻿package mortal.game.view.guildWar
{
    import __AS3__.vec.*;
    import com.mui.containers.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guildWar.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.rules.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildWarRankWindow extends BaseWindow
    {
        private var _selfGuildName:GTextFiled;
        private var _selfTowerNum:GTextFiled;
        private var _selfScore:GTextFiled;
        private var _enemyGuildName:GTextFiled;
        private var _enemyTowerNum:GTextFiled;
        private var _enemyScore:GTextFiled;
        private var _tabBarData:Array;
        private var _vcTitleItem:Vector.<TitleItem>;
        private var _resultList:Vector.<GuildWarRank>;
        private const pageSize:int = 10;
        private var _bmpDetail:GImageBitmap;
        private var _tabBar:GTabarNew;
        private var _titleBox:GBox;
        private var _tileList:GTileList;
        private var _pageSelecter:PageSelecter;
        private var _myTotalExploit:GTextFiled;
        private var _exploitShop:GButton;
        private var _myKill:GTextFiled;
        private var _myAssist:GTextFiled;
        private var _myBeKill:GTextFiled;
        private var _myRank:GTextFiled;
        private var _tips:GTextFiled;
        private static var _instance:GuildWarRankWindow;

        public function GuildWarRankWindow(param1:ILayer = null)
        {
            this._resultList = new Vector.<GuildWarRank>;
            super(param1);
            setSize(800, 527);
            title = Language.getString(11447);
            return;
        }// end function

        override protected function configUI() : void
        {
            this._tabBarData = new Array();
            this._tabBarData.push({label:Language.getString(11425), value:0, name:"0"});
            this._tabBarData.push({label:Language.getString(11426), name:"1"});
            this._tabBarData.push({label:Language.getString(11427), name:"2"});
            this._vcTitleItem = new Vector.<TitleItem>;
            this._vcTitleItem.push(new TitleItem(Language.getString(11428), 60));
            this._vcTitleItem.push(new TitleItem(Language.getString(11429), 115));
            this._vcTitleItem.push(new TitleItem(Language.getString(11430), 160));
            this._vcTitleItem.push(new TitleItem(Language.getString(11431), 60));
            this._vcTitleItem.push(new TitleItem(Language.getString(11432), 80));
            this._vcTitleItem.push(new TitleItem(Language.getString(11433), 60));
            this._vcTitleItem.push(new TitleItem(Language.getString(11434), 60));
            this._vcTitleItem.push(new TitleItem(Language.getString(11435), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11436), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11437), 65));
            super.configUI();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(9, 58, 788, 433, this, "PanelBg"));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GuildWarRankBg, 11, 61, this));
            this._selfGuildName = UIFactory.textField(Language.getString(11438), 134, 70, 80, 20, this, GlobalStyle.textFormatBai);
            this._selfTowerNum = UIFactory.textField(Language.getStringByParam(11439, "0"), 97, 99, 80, 20, this, GlobalStyle.textFormatBai, true);
            this._selfScore = UIFactory.textField(Language.getStringByParam(11440, "0", "3500"), 240, 99, 80, 20, this, GlobalStyle.textFormatBai, true);
            this._enemyGuildName = UIFactory.textField(Language.getString(11438), 568, 70, 80, 20, this, GlobalStyle.textFormatBai);
            this._enemyTowerNum = UIFactory.textField(Language.getStringByParam(11439, "0"), 530, 99, 80, 20, this, GlobalStyle.textFormatBai, true);
            this._enemyScore = UIFactory.textField(Language.getStringByParam(11440, "0", "3500"), 674, 99, 80, 20, this, GlobalStyle.textFormatBai, true);
            this._tabBar = UIFactory.gTabBarNew(2, 30, this._tabBarData, 800, 500, 65, 26, this, this.onTabBarSelectedChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            var _loc_1:int = 63;
            pushUIToDisposeVec(UIFactory.bg(10, 62 + _loc_1, 787, 22, this, "RegionTitleBg"));
            this._titleBox = ObjTeamCreate.createTitles(this._vcTitleItem, GlobalStyle.textFormatBai.center());
            UIFactory.setObjAttri(this._titleBox, 8, 62 + _loc_1, -1, -1, this);
            this._tileList = UIFactory.tileList(8, 85 + _loc_1, 785, 307, this);
            this._tileList.setStyle("cellRenderer", GuildWarRankCellRenderer);
            this._tileList.rowHeight = 30;
            this._tileList.columnWidth = 785;
            this._tileList.direction = GBoxDirection.VERTICAL;
            this._pageSelecter = UIFactory.pageSelecter(328, 398 + _loc_1, this, PageSelecter.CompleteMode);
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._myTotalExploit = UIFactory.gTextField(Language.getStringByParam(11441, "0"), 30, 438 + _loc_1, 130, 20, this, GlobalStyle.textFormatPutong);
            this._exploitShop = UIFactory.gButton(Language.getString(11442), 165, 436 + _loc_1, 65, 22, this, "GreenLinkButton");
            this._myKill = UIFactory.gTextField(Language.getStringByParam(11443, "0"), 257, 438 + _loc_1, 80, 20, this, GlobalStyle.textFormatHuang);
            this._myAssist = UIFactory.gTextField(Language.getStringByParam(11444, "0"), 340, 438 + _loc_1, 80, 20, this, GlobalStyle.textFormatHuang);
            this._myRank = UIFactory.gTextField(Language.getStringByParam(11445, "100"), 410, 438 + _loc_1, 140, 20, this, GlobalStyle.textFormatPutong);
            this._tips = UIFactory.gTextField(Language.getString(11446), 550, 438 + _loc_1, 140, 20, this, GlobalStyle.textFormatPutong);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarRequestRankList));
            this._exploitShop.configEventListener(MouseEvent.CLICK, this.onClickHonorShop);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarGuildScore, this.onGuildWarGuildScore);
            NetDispatcher.addCmdListener(ServerCommand.ExploitUpdate, this.onExploitUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarRankListUpdate, this.onRankUpdate);
            this.updateTotalScore();
            return;
        }// end function

        protected function onGuildWarGuildScore(param1) : void
        {
            this.updateTotalScore();
            return;
        }// end function

        protected function updateTotalScore() : void
        {
            if (Cache.instance.guildWar.myGuildWarScore)
            {
                this._selfGuildName.text = Cache.instance.guildWar.myGuildWarScore.name;
                this._selfTowerNum.htmlText = Language.getStringByParam(11439, Cache.instance.guildWar.myGuildWarScore.occupy.toString());
                this._selfScore.htmlText = Language.getStringByParam(11440, Cache.instance.guildWar.myGuildWarScore.score.toString(), GameConst.GuildWarMaxScore.toString());
                if (Cache.instance.guildWar.enemyGuildWarScore)
                {
                    this._enemyGuildName.text = Cache.instance.guildWar.enemyGuildWarScore.name;
                    this._enemyTowerNum.htmlText = Language.getStringByParam(11439, Cache.instance.guildWar.enemyGuildWarScore.occupy.toString());
                    this._enemyScore.htmlText = Language.getStringByParam(11440, Cache.instance.guildWar.enemyGuildWarScore.score.toString(), GameConst.GuildWarMaxScore.toString());
                }
            }
            return;
        }// end function

        protected function onExploitUpdate(param1) : void
        {
            this._myTotalExploit.htmlText = Language.getStringByParam(11441, Cache.instance.role.money.exploit.toString());
            return;
        }// end function

        protected function onClickHonorShop(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.NPC_OpenNpcShop, ShopType.Exploit));
            return;
        }// end function

        private function onRankUpdate(param1 = null) : void
        {
            this.updateScore();
            return;
        }// end function

        public function updateScore() : void
        {
            this._tabBar.selectedIndex = 0;
            this.onTabBarSelectedChange();
            this.updateMyScore();
            this._myTotalExploit.htmlText = Language.getStringByParam(11441, Cache.instance.role.money.exploit.toString());
            return;
        }// end function

        protected function updateMyScore() : void
        {
            var _loc_1:* = Cache.instance.guildWar.myResult;
            if (_loc_1)
            {
                this._myKill.text = Language.getStringByParam(11443, _loc_1.score.kill.toString());
                this._myAssist.text = Language.getStringByParam(11444, _loc_1.score.assist.toString());
                this._myRank.text = Language.getStringByParam(11445, _loc_1.rank.toString());
            }
            return;
        }// end function

        protected function onTabBarSelectedChange(event:Event = null) : void
        {
            this._pageSelecter.currentPage = 1;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    this._resultList = Cache.instance.guildWar.guildWarTotalRank;
                    break;
                }
                case 1:
                {
                    this._resultList = Cache.instance.guildWar.guildWarSelfRank;
                    break;
                }
                case 2:
                {
                    this._resultList = Cache.instance.guildWar.guildWarEnemyRank;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._pageSelecter.maxPage = Math.ceil(this._resultList.length / 10);
            this.updateList();
            return;
        }// end function

        protected function onPageChange(event:Event = null) : void
        {
            this.updateList();
            return;
        }// end function

        protected function updateList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * this.pageSize;
            var _loc_3:* = Math.min(this.pageSize * this._pageSelecter.currentPage, this._resultList.length);
            var _loc_4:* = _loc_2;
            while (_loc_4 < _loc_3)
            {
                
                _loc_1.addItem(this._resultList[_loc_4]);
                _loc_4++;
            }
            this._tileList.dataProvider = _loc_1;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._resultList = new Vector.<GuildWarRank>;
            this._selfGuildName.dispose(param1);
            this._selfGuildName = null;
            this._selfTowerNum.dispose(param1);
            this._selfTowerNum = null;
            this._selfScore.dispose(param1);
            this._selfScore = null;
            this._enemyGuildName.dispose(param1);
            this._enemyGuildName = null;
            this._enemyTowerNum.dispose(param1);
            this._enemyTowerNum = null;
            this._enemyScore.dispose(param1);
            this._enemyScore = null;
            this._tabBar.dispose(param1);
            this._tabBar = null;
            this._titleBox.dispose(param1);
            this._titleBox = null;
            this._tileList.dispose(param1);
            this._tileList = null;
            this._pageSelecter.dispose(param1);
            this._pageSelecter = null;
            this._myTotalExploit.dispose(param1);
            this._myTotalExploit = null;
            this._myKill.dispose(param1);
            this._myKill = null;
            this._myAssist.dispose(param1);
            this._myAssist = null;
            this._myRank.dispose(param1);
            this._myRank = null;
            this._tips.dispose(param1);
            this._tips = null;
            NetDispatcher.removeCmdListener(ServerCommand.GuildWarGuildScore, this.onGuildWarGuildScore);
            NetDispatcher.removeCmdListener(ServerCommand.ExploitUpdate, this.onExploitUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.GuildWarRankListUpdate, this.onRankUpdate);
            super.disposeImpl(param1);
            return;
        }// end function

        public static function get instance() : GuildWarRankWindow
        {
            if (!_instance)
            {
                _instance = new GuildWarRankWindow;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
